﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.API.Application.Commands.Week03.leo;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week03.wenk
{
    public class OilyfatDelHandler : IRequestHandler<OilyfatDelCommend, Result>
    {
        private readonly IBaseRepository<Oilyfat> Oilyfat;

        public OilyfatDelHandler(IBaseRepository<Oilyfat> Oilyfat)
        {
            this.Oilyfat = Oilyfat;
        }

        public async Task<Result> Handle(OilyfatDelCommend request, CancellationToken cancellationToken)
        {
            var str = new Result() { Code = (int)CommonErrorCode.Delete };

            try
            {
                var list = await Oilyfat.GetAll().Where(x => x.OilyfatId == request.OilyfatId).FirstAsync();
                if (list != null)
                {
                    list.IsDelete = true;
                }
                var info = Oilyfat.Update(list);
                if (info != null)
                {
                    str.Code = (int)CommonErrorCode.Success;
                    str.Msg = "删除成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return str;
        }
    }
}
